﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using OpenLab.Model;

namespace OpenLab.DAL
{
	public partial class BrancheDAL
	{
        public Branche Add
			(Branche branche)
		{
				string sql ="INSERT INTO Branche (designation, ordre)  output inserted.id VALUES (@designation, @ordre)";
				SqlParameter[] para = new SqlParameter[]
					{
						new SqlParameter("@designation", ToDBValue(branche.designation)),
						new SqlParameter("@ordre", ToDBValue(branche.ordre)),
					};
					
				int newId = (int)SqlHelper.ExecuteScalar(sql, para);
				return GetById(newId);
		}

        public int DeleteById(int id)
		{
            string sql = "DELETE Branche WHERE Id = @Id";

           SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};
		
            return SqlHelper.ExecuteNonQuery(sql, para);
		}
		
				
        public int Update(Branche branche)
        {
            string sql =
                "UPDATE Branche " +
                "SET " +
			" designation = @designation" 
                +", ordre = @ordre" 
               
            +" WHERE id = @id";


			SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@id", branche.id)
					,new SqlParameter("@designation", ToDBValue(branche.designation))
					,new SqlParameter("@ordre", ToDBValue(branche.ordre))
			};

			return SqlHelper.ExecuteNonQuery(sql, para);
        }		
		
        public Branche GetById(int id)
        {
            string sql = "SELECT * FROM Branche WHERE Id = @Id";
            using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql, new SqlParameter("@Id", id)))
			{
				if (reader.Read())
				{
					return ToModel(reader);
				}
				else
				{
					return null;
				}
       		}
        }
		
		public Branche ToModel(SqlDataReader reader)
		{
			Branche branche = new Branche();

			branche.id = (int)ToModelValue(reader,"id");
			branche.designation = (string)ToModelValue(reader,"designation");
			branche.ordre = (int)ToModelValue(reader,"ordre");
			return branche;
		}
		
		public int GetTotalCount()
		{
			string sql = "SELECT count(*) FROM Branche";
			return (int)SqlHelper.ExecuteScalar(sql);
		}
		
		public IEnumerable<Branche> GetPagedData(int minrownum,int maxrownum)
		{
			string sql = "SELECT * from(SELECT *,row_number() over(order by id) rownum FROM Branche) t where rownum>=@minrownum and rownum<=@maxrownum";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql,
				new SqlParameter("@minrownum",minrownum),
				new SqlParameter("@maxrownum",maxrownum)))
			{
				return ToModels(reader);					
			}
		}
		
		public IEnumerable<Branche> GetAll()
		{
			string sql = "SELECT * FROM Branche";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql))
			{
				return ToModels(reader);			
			}
		}
		
		protected IEnumerable<Branche> ToModels(SqlDataReader reader)
		{
			var list = new List<Branche>();
			while(reader.Read())
			{
				list.Add(ToModel(reader));
			}	
			return list;
		}		
		
		protected object ToDBValue(object value)
		{
			if(value==null)
			{
				return DBNull.Value;
			}
			else
			{
				return value;
			}
		}
		
		protected object ToModelValue(SqlDataReader reader,string columnName)
		{
			if(reader.IsDBNull(reader.GetOrdinal(columnName)))
			{
				return null;
			}
			else
			{
				return reader[columnName];
			}
		}
	}
}
